{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e8708058",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person:\n",
    "    def __init__(self, name, age):\n",
    "        # 定义类的成员\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    # 类方法演示：创建默认Person对象\n",
    "    @classmethod\n",
    "    def default_person(cls):\n",
    "        \"\"\"类方法：创建一个默认的Person实例\"\"\"\n",
    "        print(\"调用类方法创建默认对象\")\n",
    "        return cls(name=\"默认名称\", age=18)  # 通过类名cls创建实例\n",
    "\n",
    "    # 类方法演示：格式化天气信息（支持关键字参数乱序）\n",
    "    @classmethod\n",
    "    def format_weather(cls, *, city: str, date: str):\n",
    "        \"\"\"类方法：格式化天气信息（必须使用关键字参数）\"\"\"\n",
    "        result = f\"{city}市{date}的天气晴朗\"\n",
    "        print(f\"天气信息：{result}\")\n",
    "        return result\n",
    "\n",
    "    # 类方法演示：带更多参数的格式化（支持关键字参数乱序）\n",
    "    @classmethod\n",
    "    def format_detailed_weather(cls, *, city: str, date: str, road: str):\n",
    "        \"\"\"类方法：带详细地址的天气格式化（必须使用关键字参数）\"\"\"\n",
    "        result = f\"{city}市{road}{date}的天气多云\"\n",
    "        print(f\"详细天气信息：{result}\")\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11f895b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用类方法创建默认对象\n",
      "默认对象：默认名称, 18岁\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 演示1：类方法直接通过类名调用（无需创建实例）\n",
    "default = Person.default_person()\n",
    "print(f\"默认对象：{default.name}, {default.age}岁\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "edbb119a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== 天气信息测试 =====\n",
      "天气信息：北京市2023-10-01的天气晴朗\n",
      "天气信息：上海市2023-10-02的天气晴朗\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'上海市2023-10-02的天气晴朗'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示2：关键字参数乱序调用（不影响结果）\n",
    "print(\"===== 天气信息测试 =====\")\n",
    "# 正常顺序\n",
    "Person.format_weather(city=\"北京\", date=\"2023-10-01\")\n",
    "# 乱序\n",
    "Person.format_weather(date=\"2023-10-02\", city=\"上海\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8378b2a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "===== 详细天气信息测试 =====\n",
      "详细天气信息：广州市天河路2023-10-03的天气多云\n",
      "详细天气信息：北京市长安街2023-10-04的天气多云\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'北京市长安街2023-10-04的天气多云'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 演示3：多参数乱序调用\n",
    "print(\"\\n===== 详细天气信息测试 =====\")\n",
    "# 正常顺序\n",
    "Person.format_detailed_weather(city=\"广州\", date=\"2023-10-03\", road=\"天河路\")\n",
    "# 乱序\n",
    "Person.format_detailed_weather(road=\"长安街\", date=\"2023-10-04\", city=\"北京\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain-course (3.10.12)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
